package com.icss.day03;

import java.util.Objects;

public class Test14 {

    public static void main(String[] args) {
        Student stu1 = new Student(1, "tom");
        Student stu2 = new Student(2, "jerry");
        Student stu3 = new Student(1, "tom");

        // stu1 和 stu3
        System.out.println(stu1 == stu3);
        // equals比较是对象的值
        System.out.println(stu1.equals(stu3));
        stu1 = null;
        stu2 = null;
        stu3 = null;

        // 通知jvm 可以回收
        System.gc();


    }
}
class Student{
    private int sno;
    private String sname;

    @Override
    public String toString() {
        return "Student{" +
                "sno=" + sno +
                ", sname='" + sname + '\'' +
                '}';
    }

    @Override
    protected void finalize() throws Throwable {

        System.out.println(this + "被回收了");
        super.finalize();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return sno == student.sno && Objects.equals(sname, student.sname);
    }

    @Override
    public int hashCode() {
        return Objects.hash(sno, sname);
    }

    public Student() {
    }

    public Student(int sno, String sname) {
        this.sno = sno;
        this.sname = sname;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }
}